//
//  XDGetCityHeaderView.m
//  xindailianmeng
//
//  Created by 张东 on 2017/3/8.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDGetCityHeaderView.h"

@interface XDGetCityHeaderView ()

@property (nonatomic, strong) NSArray *hotArr;

@property (nonatomic, strong) NSArray *moreArr;

@property (nonatomic, weak) UIButton *beforeBtn;

@end


@implementation XDGetCityHeaderView

- (NSArray *)hotArr {
    if (_hotArr == nil) {
//        {
//            "province": "陕西省",
//            "city": "宝鸡市",
//            "cityCode": "7930"
//        },
        _hotArr = @[
                    @{@"province":@"北京市",@"city":@"北京",@"cityCode":@"1000"},
                    @{@"province":@"上海市",@"city":@"上海",@"cityCode":@"2900"},
                    @{@"province":@"浙江省",@"city":@"杭州",@"cityCode":@"3310"},
                    @{@"province":@"广东省",@"city":@"广州",@"cityCode":@"5810"},
                    @{@"province":@"四川省",@"city":@"成都",@"cityCode":@"6510"},
                    @{@"province":@"江苏省",@"city":@"苏州",@"cityCode":@"3050"}
                    ];
        
    }
    return _hotArr;
}

- (NSArray *)moreArr {
    if (_moreArr == nil) {
        _moreArr = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"J",@"K",@"L",@"M",@"N",@"P",@"Q",@"R",@"S",@"T",@"W",@"X",@"Y",@"Z"];
    }
    return _moreArr;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

    [self setupUI];
//        self = [[NSBundle mainBundle] loadNibNamed:@"XDGetCityHeaderView" owner:nil options:nil].firstObject;
    }
    return self;
}
- (void)setupUI {
    
    self.backgroundColor = kColorRGB(234, 234, 234);
    [self lableWithTitel:@"热门城市" frame:CGRectMake(15, 0, kScreenW, 33)];
    
    UIView *hotView = [[UIView alloc] initWithFrame:CGRectMake(0, 33, kScreenW, 114)];
    hotView.backgroundColor = [UIColor whiteColor];
    [self addSubview:hotView];
    
    for (NSInteger i = 0; i < 6; i++) {
        
        CGFloat BtnW = (kScreenW -  30 - 48) / 3;
        CGFloat BtnH = 33;
        CGFloat btnY = (i / 3) * (BtnH + 18) + 15;
        CGFloat BtnX = (i % 3) * (BtnW + 24) + 15;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(BtnX, btnY,BtnW, BtnH)];
        [btn setBackgroundImage:[UIImage imageNamed:@"backGrourd_city_nor"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"backGrourd_more_nor"] forState:UIControlStateHighlighted];
        [btn setTitle:self.hotArr[i][@"city"] forState:UIControlStateNormal];
        [btn setTitleColor:kColorRGB(51, 51, 51) forState:UIControlStateNormal];
        btn.titleLabel.font = kBaseFont_(14);
        [btn addTarget:self action:@selector(hotCityClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
        [hotView addSubview:btn];
    }
     [self lableWithTitel:@"更多城市" frame:CGRectMake(15, CGRectGetMaxY(hotView.frame), kScreenW, 33)];
    
    UIView *moreView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(hotView.frame) + 33, kScreenW, 219)];
    moreView.backgroundColor = [UIColor whiteColor];
    [self addSubview:moreView];
    
    for (NSInteger i = 0; i < 22; i++) {
        
        CGFloat BtnW = (kScreenW -  30 - 19 * 5) / 6;
        CGFloat BtnH = 34;
        CGFloat btnY = (i / 6) * (BtnH + 18) + 15;
        CGFloat BtnX = (i % 6) * (BtnW + 19) + 15;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(BtnX, btnY,BtnW, BtnH)];
        [btn setBackgroundImage:[UIImage imageNamed:@"backGrourd_more"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"backGrourd_city"] forState:UIControlStateSelected];
        [btn setTitle:self.moreArr[i] forState:UIControlStateNormal];
        [btn setTitleColor:kColorRGB(51, 51, 51) forState:UIControlStateNormal];
        [btn setTitleColor:kColorRGB(187,147,115) forState:UIControlStateHighlighted];
        btn.titleLabel.font = kBaseFont_(14);
        [btn addTarget:self action:@selector(moreCityClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 200 + i;
        [moreView addSubview:btn];
    }
    
}


- (void)lableWithTitel:(NSString *)title frame:(CGRect)frame{
    UILabel *hotCity = [[UILabel alloc] initWithFrame:frame];
    hotCity.text = title;
    hotCity.textAlignment = NSTextAlignmentLeft;
    hotCity.textColor = kColorRGB(102, 102, 102);
    hotCity.font = kBaseFont_(14);
    [self addSubview:hotCity];
}

- (void)hotCityClick:(UIButton *)hotCityBtn {
    
    if ([self.delegate respondsToSelector:@selector(hotCityClickWithXDGetCityHeaderView:hotCityDic:)]) {
        
        NSDictionary *dic = self.hotArr[hotCityBtn.tag - 100];
        [self.delegate hotCityClickWithXDGetCityHeaderView:self hotCityDic:dic];
    }
    
}

- (void)moreCityClick:(UIButton *)moreCityBtn {
    
    self.beforeBtn.selected = NO;
    moreCityBtn.selected = !moreCityBtn.selected;
    self.beforeBtn = moreCityBtn;
    if ([self.delegate respondsToSelector:@selector(moreCityClickWithXDGetCityHeaderView:moreCityStr:)]) {
        [self.delegate moreCityClickWithXDGetCityHeaderView:self moreCityStr:self.moreArr[moreCityBtn.tag - 200]];
    }
}


@end
